package edu.fhsu.csci468;

import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;

public class ConnectBox extends JFrame {

	private static final long serialVersionUID = 1L;
	private JDesktopPane jDesktopPane = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JLabel jLabel = null;
	private JComboBox jComboBox = null;
	private JLabel jLabel1 = null;

    public void closeWindow()
    {
    	this.dispose();
    }
	
	/**
	 * This is the default constructor
	 */
	public ConnectBox() {
		super();
        this.setLocationRelativeTo(null);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(264, 264);
		this.setContentPane(getJDesktopPane());
		this.setTitle("Connect To Server");
	}

	/**
	 * This method initializes jDesktopPane	
	 * 	
	 * @return javax.swing.JDesktopPane	
	 */
	private JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(45, 90, 151, 31));
			jLabel1.setText("Choose sensor type");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(45, 15, 121, 31));
			jLabel.setText("Enter IP Address");
			jDesktopPane = new JDesktopPane();
			jDesktopPane.add(getJTextField(), null);
			jDesktopPane.add(getJButton(), null);
			jDesktopPane.add(jLabel, null);
			jDesktopPane.add(getJComboBox(), null);
			jDesktopPane.add(jLabel1, null);
		}
		return jDesktopPane;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField("127.0.0.1");
			jTextField.setBounds(new Rectangle(45, 45, 166, 31));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.addActionListener(new Connect());
			jButton.setBounds(new Rectangle(45, 180, 166, 32));
			jButton.setText("Connect");
		}
		return jButton;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.addItem(new SensorKind(SensorType.CarbonMonoxide,
					"Carbon Monoxide Sensor"));
			jComboBox.addItem(new SensorKind(SensorType.Door,
					"Door Sensor"));
			jComboBox.addItem(new SensorKind(SensorType.Flood,
					"Flood"));
			jComboBox.addItem(new SensorKind(SensorType.Smoke,
					"Smoke"));
			jComboBox.addItem(new SensorKind(SensorType.Window,
					"Window"));
			jComboBox.setBounds(new Rectangle(45, 120, 166, 31));
		}
		return jComboBox;
	}
	
    private class Connect implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		String ip = jTextField.getText();
    		SensorGUI sensorGUI = new SensorGUI(ip, jComboBox.getSelectedItem());
    		sensorGUI.setVisible(true);
    		sensorGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		closeWindow();

    	}
    }

}  //  @jve:decl-index=0:visual-constraint="249,43"

